<?php       
header('content-type:html/text;charset=utf-8');
// //1. 创建画布 
// $im = imageCreateTrueColor(200, 200);          //建立空白背景
// $white = imageColorAllocate ($im, 255, 255, 255);    //设置绘图颜色
// $blue  = imageColorAllocate ($im, 0, 0, 64);
// //2. 开始绘画
// imageFill($im, 0, 0, $blue);                            //绘制背景
// imageLine($im, 0, 0, 200, 200, $white);            //画线
// imageString($im, 4, 50, 150, 'Sales', $white);      //添加字串
// //3. 输出图像
// header('Content-type: image/png');
// imagePng ($im);     //以 PNG 格式将图像输出
// //4. 释放资源
// imageDestroy($im);//  
// $width=300;
// $height=150;
// $num=4;
// $huabu=imageCreateTrueColor($width,200);//建空白背景
// $color=imagecolorallocate($huabu,0,255,255);
// imageFill($huabu,0,0,$color);

// for($i=1;$i<=1000;$i++){
//     $colore=imageColorAllocate($huabu,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//     imagesetpixel($huabu,mt_rand(0,$width),mt_rand(0,$width), $colore);}

// for($p=0;$p<101;$p++){
//     $colory=imageColorAllocate($huabu,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//     imageLine($huabu,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$width),$colory);
// }
// for($o=0;$o<$num;$o++){
//     $arr='3456789qwertyuiopasdfghjklzxcvbnm';
//     $z=$arr[mt_rand(0,(strlen($arr)-1))];
//      imagettftext($huabu,50,0,$o*50,$height,$colore,'Microsoft+yahei.ttf',"$z");
//     }

//     header('content-type:image/png');
//     imagepng($huabu);
//     imageDestroy($huabu);
$width=300;
$height=100;
$num=4;
//画布
$yzm=imagecreatetruecolor($width,$height);
$bgc=imagecolorallocate($yzm, 250, 250, 250);
$color1=imagecolorallocate($yzm,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
//绘制图像
imageFill($yzm,0,0,$bgc); //背景色
                  //画点
for($i=1;$i<=1000;$i++){           
    $color1=imagecolorallocate($yzm,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagesetpixel($yzm, mt_rand(0,$width), mt_rand(0,$height), $color1);
}
             //划线
for($i=1;$i<=20;$i++){
 $color1=imagecolorallocate($yzm,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageLine($yzm,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$color1);
}
           //输出验证码
  $rand='';   
for($i=0;$i<$num;$i++){
    $str="3456789qwertyuipasdfghjkxcvbnm";
    $color1=imagecolorallocate($yzm,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
    //$z= ($str[mt_rand(0,(strlen($str)-1))]);
    $arr1=str_split($str,1);
    $z= ($arr1[mt_rand(0,(count($arr1)-1))]);
  imagettftext($yzm,70,mt_rand(-15,30),20+($width/$num)*$i, (($height+60)/2),$color1,'Microsoft+yahei.ttf',$z);
$rand.=$z;
}
session_start();
$_SESSION['yzm']=$rand;
// die;
//输出图像
header('content-type:image/png');
imagepng($yzm);
//释放资源 销毁
imagedestroy($yzm);
?>